package tictactoe;

//~--- non-JDK imports --------------------------------------------------------

import tictactoe.util.Constants;

//~--- JDK imports ------------------------------------------------------------

import java.awt.Point;

/**
 *
 * @author buya
 */
public class Game {
    private Player cross, circle;
    private Desk   desk;
    private int    playing;
    private int    turn;

    /**
     *
     * @param cross
     * @param circle
     */
    public Game(Player cross, Player circle) {
        this(cross, circle, Constants.CROSS);
    }

    /**
     *
     * @param cross
     * @param circle
     * @param playing
     */
    public Game(Player cross, Player circle, int playing) {
        desk         = new Desk(16, 16);
        this.cross   = cross;
        this.circle  = circle;
        this.playing = playing;
        turn         = 0;
    }

    /**
     *
     */
    public void startGame() {
        playing = Constants.CROSS;
        turn    = 0;
    }

    /**
     *
     * @param where
     * @return
     */
    public boolean turn(Point where) {
        if (turn(playing, where)) {
            turn++;

            if (playing == Constants.CIRCLE) {
                playing = Constants.CROSS;
            } else {
                playing = Constants.CIRCLE;
            }

            return true;
        }

        return false;
    }

    /**
     *
     * @return
     */
    public Player getCross() {
        return cross;
    }

    /**
     *
     * @return
     */
    public Player getCircle() {
        return circle;
    }

    /**
     *
     * @return
     */
    public Player getWinnerPlayer() {
        switch (desk.getWinner()) {
        case Desk.CIRCLE :
            return circle;

        case Desk.CROSS :
            return cross;

        default :
            return null;
        }
    }

    /**
     *
     * @return
     */
    public int getWinner() {
        return desk.getWinner();
    }

    /**
     *
     * @return
     */
    public int getTurnCount() {
        return turn;
    }

    /**
     *
     * @return
     */
    public int getPlaying() {
        return playing;
    }

    /**
     *
     * @return
     */
    public Player whoseTurn() {
        if (playing == Constants.CROSS) {
            return cross;
        }

        if (playing == Constants.CIRCLE) {
            return circle;
        }

        return null;
    }

    /**
     *
     * @return
     */
    public Desk getDeskCopy() {
        return (Desk) desk.clone();
    }

    private boolean turn(int player, Point where) {
        if (desk.canPlace(where)) {
            return desk.add(player, where);
        } else {
            return false;
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
